package com.bangdream.michelia.presenter;

import android.app.Dialog;

import com.bangdream.michelia.contract.LiveContract;
import com.bangdream.michelia.contract.MessageContract;
import com.bangdream.michelia.entity.LiveBean;
import com.bangdream.michelia.entity.MessageBean;
import com.bangdream.michelia.model.ILiveModelImpl;
import com.bangdream.michelia.model.IMessageModelImpl;
import com.bangdream.michelia.presenter.base.BasePresenter;
import com.bangdream.michelia.tool.rejava.BaseObserverEntityNormalHttp;
import com.bangdream.michelia.tool.retrofithttp.TokenManagerDef;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @date on 2018/8/1 23:38
 * @email 909732184@qq.com
 * 站内消息
 */
public class MessagePersenter<V> extends BasePresenter<V> {
    private MessageContract.ICoursesModel model;

    public MessagePersenter() {
        this.model = new IMessageModelImpl();
    }
    public void getUserMessageList(final int pageNo, int pageSize, int type, Dialog dialog){
        if(getMyView()==null)
            return;
        Map paramsMap = new HashMap();
        setPd(dialog);
        paramsMap.put("pageNo",pageNo);
        paramsMap.put("pageSize",pageSize);
//        paramsMap.put("userId",userId);
        paramsMap.put("type",type);
        TokenManagerDef.work(() -> model.getUserMessageList(paramsMap)).compose(TokenManagerDef.newInstance().getTransformer())
                .compose(composeFunction)

                .subscribe(new BaseObserverEntityNormalHttp<List<MessageBean>>(this,pd) {
                    @Override
                    public void onHandleSuccess(List<MessageBean> beans) {
                        if(getMyView()==null)
                            return;
                        getMyView().setUserMessageList(true,pageNo,beans,"");
                    }
                    @Override
                    public void onHandleError(int code, String message) {
                        getMyView().setUserMessageList(false,pageNo,null,null);
                    }
                });
    }

    public void getSystemMessageList(final int pageNo, int pageSize, String userId, Dialog dialog){
        if(getMyView()==null)
            return;
        Map paramsMap = new HashMap();
        setPd(dialog);
        paramsMap.put("pageNo",pageNo);
        paramsMap.put("pageSize",pageSize);
        paramsMap.put("userId",userId);

        model.getSystemMessageList(paramsMap)
                .compose(composeFunction)
                .subscribe(new BaseObserverEntityNormalHttp<List<MessageBean>>(this,pd) {
                    @Override
                    public void onHandleSuccess(List<MessageBean> beans) {
                        if(getMyView()==null)
                            return;
                        getMyView().setUserMessageList(true,pageNo,beans,getDescribe(beans));
                    }

                    @Override
                    public void onHandleError(int code, String message) {
                        getMyView().setUserMessageList(false,pageNo,null,null);
                    }
                });
    }

    public void actionRead(String id, Dialog dialog){
        if(getMyView()==null)
            return;
        setPd(dialog);
        TokenManagerDef.work(() -> model.actionRead(id)).compose(TokenManagerDef.newInstance().getTransformer())
                .compose(composeFunction)
                .subscribe(new BaseObserverEntityNormalHttp<List<MessageBean>>(this,pd) {
                    @Override
                    public void onHandleSuccess(List<MessageBean> beans) {
                        if(getMyView()==null)
                            return;
                        getMyView().actionRead(true,getDescribe(beans));
                    }
                });
    }

    private MessageContract.IMessageView getMyView(){
        return (MessageContract.IMessageView) obtainView();
    }
}
